package com.jia.netty.channel;

import io.netty.channel.Channel;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ChannelGroup {

    /**
     * key 设备id
     * Channel 设备id的连接对象
     */
    public static Map<String, Channel> channelMap = new HashMap<>();

    /**
     * 添加channel对象到容器中
     * @param did
     * @param channel
     */
    public static void addChannel(String did,Channel channel) {
        channelMap.put(did,channel);
    }

    public static Channel getChannel(String did){
        return channelMap.get(did);
    }

    public static void removeChannel(String did) {
        channelMap.remove(did);
    }

    public static void removeChannel(Channel channel){
        boolean b = channelMap.containsValue(channel);
        if (b){
            Set<Map.Entry<String, Channel>> entries = channelMap.entrySet();
            for (Map.Entry<String, Channel> entry : entries) {
                if (entry.getValue() == channel){
                    channelMap.remove(entry.getKey());
                    break;
                }
            }
        }
    }
}
